﻿package school;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HomeServlet
 */
public class HomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JDBCOptionDAO jdbcOption = new JDBCOptionDAO();

		Set<String> listLocs = jdbcOption.getLocations();
		
		// methode avec AJAX
		String type = request.getParameter("type");
		String key_word = request.getParameter("keyWord");
		String date_start = request.getParameter("dateStart");
		String location = request.getParameter("loc");
		Set<Option> listBySearch = jdbcOption.getListBySearch(key_word, date_start, location);
		
		// data a renvoyer pour ajax
		if (type.equals("list1")){
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<option></option>");
			for(String str : listLocs) {
				out.println("<option>" + str + "</option>");
			}
		}
		else{
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			Date dateCurrent = new Date();
			SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
			
			out.println("<h3>Your List of the Courses</h3>");
			out.println("<table col=\"5\" border=\"1\"><tr>");
			out.println("<th width=\"30\"></th><th width=\"100\">Code</th><th width=\"150\">Title</th><th width=\"200\">Sessions</th><th width=\"100\">Location</th></tr>");
			
			if(listBySearch.isEmpty()){
				out.println("<tr><td colspan=\"5\">There is no course found</td></tr>");
			}
			else{
				for(Option o : listBySearch) {
					out.println("<tr>");
					if(timeFormat.format(dateCurrent).compareTo(o.getDatae_end()) > 0 ){
						out.println("<td align=\"center\"><input type=\"checkbox\" name=\"check_option\" value=\"" + o.getSession_id() + "\" class=\"timeOut\"></td>");

					}else{
						out.println("<td align=\"center\"><input type=\"checkbox\" name=\"tabCheckbox[]\" value=\"" + o.getSession_id() + "\" class=\"stillValide\"></td>");
					}	
			
					out.println("<td align=\"center\">" + o.getCode() + "</td>");
					out.println("<td align=\"center\">" + o.getTitle() + "</td>");
					out.println("<td align=\"center\">" + o.getDate_start() + " to " + o.getDatae_end() + "</td>");		
					out.println("<td align=\"center\">" + o.getLocation() + "</td>");
					out.println("</tr>");
				}
			}
			
			out.println("</table><br/><br/>");
		}
		
		// sans AJAX
//		response.setContentType("text/html");
//		PrintWriter out = response.getWriter();
//		out.println("<html>");
//		out.println("<body>");
//		out.println("<head>");
//		out.println("<title>Welcome - Your OnLine School !</title>");
//		out.println("</head>");
//		out.println("<body>");
//		out.println("<h2>Hello ! Welcome to Your OnLine School.<br/>Here you can find the interesting courses for you !</h2>");
//		out.println("<form name=\"myHomepage\" method=\"GET\" action=\"SearchServlet\">");
//		out.println("<h3>Do a research</h3>");
//		out.println("By key word <input type=\"text\" name=\"key_word\"/><br/>");
//		out.println("By date you want to start <input type=\"date\" name=\"date_start\"/><br/>");
//		out.println("By location <select name=\"list_locs\"><option></option>");
//		
//		for(String str : listLocs) {
//			out.println("<option>" + str.toString() + "</option>");
//		}
//		
//		out.println("</select><br/>");
//		out.println("<input type=\"submit\" value=\"Search\"/>");
//		out.println("</form>");
//		
//		out.println("<form name=\"myList\" method=\"POST\" action=\"RegSuccessServlet\">");
//		out.println("<h3>Your List of the Courses</h3>");
//		out.println("<table col=\"5\"><tr>");
//		out.println("<th></th><th>Code</th><th>Title</th><th>Sessions</th><th>Location</th><th></th></tr>");
//		
//		for(Option o : listOptions) {
//			out.println("<tr>");
//			out.println("<td><input type=\"checkbox\" name=\"check_option\" value=\"" + o.getSession_id() + "\" ></td>");
//			out.println("<td>" + o.getCode() + "</td>");
//			out.println("<td>" + o.getTitle() + "</td>");
//			out.println("<td>" + o.getDate_start() + " to " + o.getDatae_end() + "</td>");
//			out.println("<td>" + o.getLocation() + "</td>");
//			out.println("<td></td></tr>");
//		}
//
//		out.println("</table>");
//
//		out.println("<input type=\"submit\" value=\"Register\"/>");
//		out.println("</form>");
//		out.println("</body>");
//		out.println("</html>");

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
